package com.newtouch.krd_app.view;

import static com.newtouch.krd_app.util.ConstUtil.WidgetViewImageTime;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.newtouch.krd_app.R;
import com.newtouch.krd_app.http.api.TerminalApi;
import com.newtouch.krd_app.util.ControllerInterface;
import com.newtouch.krd_app.util.MusicServer;

import java.util.Timer;
import java.util.TimerTask;

/**
 * author : TQ
 * time   : 2023/2/14
 * desc   : 图片播放组件
 */
public class WidgetViewImage extends WidgetView{
    private ImageView imageView;
    private TerminalApi.Bean.FilesDTO filesDTO;

    /**
     * 构造函数
     *
     * @param context
     */
    public WidgetViewImage(Context context) {
        super(context);
    }

    public WidgetViewImage(Context context, ControllerInterface controllerInterface) {
        super(context, controllerInterface);
    }

    @Override
    public View initView() {
        view = View.inflate(context, R.layout.widget_image_view,null);
        imageView = view.findViewById(R.id.imageView);
        return super.initView();
    }

    @Override
    public void startPlay() {
        super.startPlay();
        if (timer == null) {
            timer = new Timer();
            timerTask = new TimerTask() {
                @Override
                public void run() {
                    controllerInterface.playEnd();
                }
            };
        }
        //delay 4000不立即执行，等第一张显示4秒后执行
        //period 4000 间隔4秒执行循环
        timer.schedule(timerTask,WidgetViewImageTime);
        if (filesDTO.getFileType().equals("pdf")){
            MusicServer.play(context,R.raw.background);
        }
    }

    @Override
    public void terminatePlay() {
        super.terminatePlay();
        if (filesDTO.getFileType().equals("pdf")){
            MusicServer.stop(context);
        }
    }

    @Override
    public View getView() {
        return super.getView();
    }

    @Override
    public TerminalApi.Bean.FilesDTO getFilesDTO() {
        return super.getFilesDTO();
    }

    @Override
    public void setFilesDTO(TerminalApi.Bean.FilesDTO filesDTO) {
        super.setFilesDTO(filesDTO);
        this.filesDTO = filesDTO;
        String url = "";
        if (filesDTO.getFileType().equals("pdf")){
            url = filesDTO.getPicFiles().get(0).getFilePath();
        }else{
            url = filesDTO.getFilePath();
        }
        Glide.with(context)
                .load(url)
                .into(imageView);
    }

    @Override
    public int getDuration() {
        return (int) WidgetViewImageTime;
    }
}
